//
//  MAPYConnectResultCell.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYConnectResultCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(mapy_bgView)
        contentView.addSubview(mapy_iconView)
        contentView.addSubview(mapy_titleLabel)
        contentView.addSubview(mapy_bottomView)
        contentView.addSubview(mapy_timeLabel)
        contentView.addSubview(mapy_successLabel)
        contentView.addSubview(mapy_iPValueLabel)
        mapy_bgView.snp.makeConstraints { make in
            make.top.left.width.equalToSuperview()
            make.height.equalTo(243)
        }
        mapy_iconView.snp.makeConstraints { make in
            make.size.equalTo(148)
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(6)
        }
        mapy_titleLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalToSuperview().offset(25)
        }
        mapy_bottomView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 332, height: 68))
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-4)
        }
        mapy_timeLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalTo(mapy_iPValueLabel.snp.bottom).offset(4)
        }
        mapy_successLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-14)
            make.top.equalTo(mapy_timeLabel.snp.bottom).offset(4)
        }
        mapy_iPValueLabel.snp.makeConstraints { make in
            make.right.equalTo(mapy_titleLabel.snp.right)
            make.top.equalTo(mapy_titleLabel.snp.bottom).offset(10)
        }
        mapy_startConnectTimer()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func mapy_connectClick() {
        MAPYManager.mapy_manager.mapy_smart = true
        MAPYManager.mapy_manager.mapy_best = false
        if MAPYManager.mapy_manager.mapy_vpnCurStateType == .mapy_connected {
            MAPYManager.mapy_manager.mapy_vpnStateClickType = .mapy_disConnect
        }else {
            MAPYManager.mapy_manager.mapy_vpnStateClickType = .mapy_connect
        }
        
        MAPYManager.mapy_manager.mapy_connectSmartAction()
        mapy_viewGetVController?.navigationController?.popToRootViewController(animated: true)
    }
    deinit {
        mapy_stopConnectTimer()
    }
    var mapy_serverModel: MAPYServerModel? {
        didSet {
            guard let mapy_serverModel = mapy_serverModel else { return  }
            mapy_iPValueLabel.text = mapy_serverModel.mapy_address
            mapy_successLabel.text = "Successfully connected to VPN\n\nstart to travel the world"
            if MAPYManager.mapy_manager.mapy_best {
                mapy_successLabel.text = "Successfully connected to\nBest servers"
            }
            if MAPYManager.mapy_manager.mapy_smart {
                mapy_successLabel.text = "Successfully connected to\nStable servers"
            }
           
            if mapy_serverModel.mapy_address == "38.114.114.19" {
                mapy_titleLabel.text = "Los Angeles"
            }
            if mapy_serverModel.mapy_address == "38.68.134.105" {
                mapy_titleLabel.text = "Dallas"
            }
            if mapy_serverModel.mapy_address == "38.107.226.61" {
                mapy_titleLabel.text = "Ashburn 3"
            }
            if mapy_serverModel.mapy_address == "216.39.248.160" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "139.64.165.239" {
                mapy_titleLabel.text = "Denver"
            }
            if mapy_serverModel.mapy_address == "38.114.114.155" {
                mapy_titleLabel.text = "Los Angeles"
            }
            if mapy_serverModel.mapy_address == "216.39.248.169" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.75.137.125" {
                mapy_titleLabel.text = "Los Angeles"
            }
            if mapy_serverModel.mapy_address == "162.251.63.93" {
                mapy_titleLabel.text = "Los Angeles"
            }
            if mapy_serverModel.mapy_address == "216.173.115.84" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.64.138.184" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.107.226.200" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.86.135.202" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.107.226.152" {
                mapy_titleLabel.text = "Ashburn 1"
            }
            if mapy_serverModel.mapy_address == "38.86.135.250" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.86.135.74" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.107.226.127" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.121.43.235" {
                mapy_titleLabel.text = "Atlanta"
            }
            if mapy_serverModel.mapy_address == "38.121.43.235" {
                mapy_titleLabel.text = "Atlanta"
            }
            if mapy_serverModel.mapy_address == "216.39.248.251" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.64.138.244" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "216.39.248.225" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.64.138.5" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.121.43.151" {
                mapy_titleLabel.text = "Atlanta 1"
            }
            if mapy_serverModel.mapy_address == "38.107.226.223" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.86.135.106" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "38.86.135.236" {
                mapy_titleLabel.text = "Ashburn"
            }
            if mapy_serverModel.mapy_address == "142.202.49.114" {
                mapy_titleLabel.text = "Ashburn 2"
            }
            if mapy_serverModel.mapy_address == "38.121.43.78" {
                mapy_titleLabel.text = "Atlanta"
            }
            if mapy_serverModel.mapy_address == "38.64.138.195" {
                mapy_titleLabel.text = "Chicago"
            }
            if mapy_serverModel.mapy_address == "38.68.135.115" {
                mapy_titleLabel.text = "Dallas"
            }
            if mapy_serverModel.mapy_address == "38.121.43.44" {
                mapy_titleLabel.text = "Atlanta 2"
            }
            if mapy_serverModel.mapy_address ==  "38.91.106.154" {
                mapy_titleLabel.text = "Miami"
            }
            if mapy_serverModel.mapy_address ==  "38.91.106.154" {
                mapy_titleLabel.text = "Miami"
            }
            if mapy_serverModel.mapy_address ==  "139.64.165.86" {
                mapy_titleLabel.text = "Denver"
            }
            if mapy_titleLabel.text?.count ?? 0 > 0 {
                mapy_serverModel.mapy_country = "USA"
                mapy_titleLabel.text = (mapy_serverModel.mapy_country ?? "") + "-" + (mapy_titleLabel.text ?? "")
            }else {
                mapy_titleLabel.text = mapy_serverModel.mapy_country
            }
        }
    }
    lazy var mapy_iPValueLabel: UILabel = {
        let mapy_valueLabel = UILabel()
        mapy_valueLabel.font = UIFont.systemFont(ofSize: 16)
        mapy_valueLabel.textColor = MAPYCommon.mapy_getHXColorValue(rgb: 0xFFFFFF)
        mapy_valueLabel.text = ""
        return mapy_valueLabel
    }()
    lazy var mapy_iconView: UIImageView = {
        let mapy_iconView = UIImageView(image: UIImage(named: "mapy_result_connectIcon"))
        return mapy_iconView
    }()
    lazy var mapy_titleLabel: UILabel = {
        let mapy_titleLabel = UILabel()
        mapy_titleLabel.font = UIFont.systemFont(ofSize: 10)
        mapy_titleLabel.textColor = MAPYCommon.mapy_getHXColorValue(rgb: 0xFFFFFF)
        mapy_titleLabel.text = ""
        return mapy_titleLabel
    }()
    lazy var mapy_bgView: UIImageView = {
        let mapy_bgView = UIImageView(image: UIImage(named: "mapy_result_connectbg"))
        return mapy_bgView
    }()
    lazy var mapy_bottomView: UIImageView = {
        let mapy_bottomView = UIImageView(image: UIImage(named: "mapy_result_connectBottom"))
        mapy_bottomView.mapy_addTap(target: self, action: #selector(mapy_connectClick))
        return mapy_bottomView
    }()
    lazy var mapy_timeLabel: UILabel = {
        let mapy_timeLabel = UILabel()
        mapy_timeLabel.font = UIFont.systemFont(ofSize: 24)
        mapy_timeLabel.textColor = MAPYCommon.mapy_getHXColorValue(rgb: 0xFFFFFF)
        mapy_timeLabel.text = "00:01:00"
        return mapy_timeLabel
    }()
    lazy var mapy_successLabel: UILabel = {
        let mapy_successLabel = UILabel()
        mapy_successLabel.font = UIFont.systemFont(ofSize: 12)
        mapy_successLabel.textColor = MAPYCommon.mapy_getHXColorValue(rgb: 0xFFFFFF)
        mapy_successLabel.text = "Successfully connected to VPN\n\nstart to travel the world"
        mapy_successLabel.numberOfLines = 0
        mapy_successLabel.textAlignment = .right
        return mapy_successLabel
    }()
    var mapy_connectName: String = "mapy_connectName"
    var mapy_connectCount:NSInteger = 0
}

extension MAPYConnectResultCell {
    fileprivate func mapy_startConnectTimer() {
        mapy_connectName = MAPYTimerCommon.mapy_timeTask(self, selector: #selector(mapy_connectSum), start: 0, interval: 1, repeats: true, async: false, name: mapy_connectName)
    }
    
    func mapy_stopConnectTimer() {
        MAPYTimerCommon.mapy_cancelTask(mapy_connectName)
        mapy_connectName = "mapy_connectName"
        mapy_connectCount = 0
    }
    
    @objc fileprivate func mapy_connectSum() {
        mapy_connectCount = mapy_connectCount + 1
        mapy_timeLabel.text = mapy_connectCount.mapy_formatTime()
    }
    
}
